package iamutkarshtiwari.github.io.ananas.editimage.fragment.paint;

import iamutkarshtiwari.github.io.ananas.ResourceTable;
import iamutkarshtiwari.github.io.ananas.editimage.adapter.ColorPickerAdapter;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.Window;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

public class BrushConfigDialog extends CommonDialog implements Slider.ValueChangedListener {

    private Properties mProperties;

    /**
     * 属性改变接口
     */
    public interface Properties {
        void onColorChanged(int colorCode);

        void onOpacityChanged(int opacity);

        void onBrushSizeChanged(int brushSize);
    }

    /**
     * 构造
     *
     * @param context context对象
     */
    public BrushConfigDialog(Context context) {
        super(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_fragment_brush_config, null, false);
        setContentCustomComponent(component);
        setCornerRadius(0);
        component.setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, 800));

        Window window = getWindow();
        //        w.setBackgroundColor(new RgbColor(RgbColor.fromArgbInt(Color.getIntColor("#77000000"))));
        window.setLayoutAlignment(TextAlignment.BOTTOM);
        Slider sizeSlider = (Slider) component.findComponentById(ResourceTable.Id_sbSize);
        Slider opacitySlider = (Slider) component.findComponentById(ResourceTable.Id_sbOpacity);
        ListContainer container = (ListContainer) component.findComponentById(ResourceTable.Id_rvColors);

        container.setItemProvider(new ColorPickerAdapter(context, this, initColor()));

        sizeSlider.setValueChangedListener(this);
        opacitySlider.setValueChangedListener(this);

        setDialogListener(() -> {
            hide();
            return true;
        });
    }

    private List<Integer> initColor() {
        List<Integer> list = new ArrayList<>();

        list.add(Color.getIntColor("#ffF2F3F4"));
        list.add(Color.getIntColor("#ff1d1d1d"));
        list.add(Color.getIntColor("#ffebce2b"));
        list.add(Color.getIntColor("#ff702c8c"));
        list.add(Color.getIntColor("#ffdb6917"));
        list.add(Color.getIntColor("#ff96cde6"));
        list.add(Color.getIntColor("#ffba1c30"));
        list.add(Color.getIntColor("#ffc0bd7f"));
        list.add(Color.getIntColor("#ff7f7e80"));
        list.add(Color.getIntColor("#ff5fa641"));
        list.add(Color.getIntColor("#ffd485b2"));
        list.add(Color.getIntColor("#ff4277b6"));
        list.add(Color.getIntColor("#ffdf8461"));
        list.add(Color.getIntColor("#ff463397"));
        list.add(Color.getIntColor("#ffe1a11a"));
        list.add(Color.getIntColor("#ff91218c"));
        list.add(Color.getIntColor("#ffe8e948"));
        list.add(Color.getIntColor("#ff7e1510"));
        list.add(Color.getIntColor("#ff92ae31"));
        list.add(Color.getIntColor("#ff6f340d"));
        list.add(Color.getIntColor("#ffd32b1e"));
        list.add(Color.getIntColor("#ff2b3514"));

//        list.add(Color.getIntColor("#ffF2F3F4"));
//        list.add(Color.getIntColor("#ff1d1d1d"));
//        list.add(Color.getIntColor("#ffebce2b"));
//        list.add(Color.getIntColor("#ff702c8c"));
//        list.add(Color.getIntColor("#ffdb6917"));
//        list.add(Color.getIntColor("#ff96cde6"));
//        list.add(Color.getIntColor("#ffba1c30"));
//        list.add(Color.getIntColor("#ffc0bd7f"));
//        list.add(Color.getIntColor("#ff702c8c"));
//        list.add(Color.getIntColor("#ff7f7e80"));
//
//        list.add(Color.getIntColor("#ff5fa641"));
//        list.add(Color.getIntColor("#ffd485b2"));
//        list.add(Color.getIntColor("#ff4277b6"));
//        list.add(Color.getIntColor("#ffdf8461"));
//        list.add(Color.getIntColor("#ff463397"));
//        list.add(Color.getIntColor("#ff91218c"));
//        list.add(Color.getIntColor("#ffe8e948"));
//        list.add(Color.getIntColor("#ff7e1510"));
//        list.add(Color.getIntColor("#ff92ae31"));
//        list.add(Color.getIntColor("#ff6f340d"));
//
//        list.add(Color.getIntColor("#ffd32b1e"));
//        list.add(Color.getIntColor("#ff2b3514"));

        return list;
    }


    void setPropertiesChangeListener(Properties properties) {
        mProperties = properties;
    }

    /**
     * 改变颜色
     *
     * @param position 位置
     */
    public void changColor(int position) {
        if (mProperties != null) {
            mProperties.onColorChanged(initColor().get(position));
        }
        hide();
    }

    @Override
    public void onProgressUpdated(Slider slider, int i, boolean b) {

        int id = slider.getId();
        if (id == ResourceTable.Id_sbOpacity) {
            if (mProperties != null) {
                mProperties.onOpacityChanged(i);
            }
        } else if (id == ResourceTable.Id_sbSize) {
            if (mProperties != null) {
                mProperties.onBrushSizeChanged(i);
            }
        }
    }

//    @Override
//    protected void onShow() {
//        super.onShow();
//        Window window = getWindow();
//        window.setStatusBarColor(Color.getIntColor("#808080"));
//        window.setNavigationBarColor(Color.getIntColor("#656565"));
//    }
//
//    @Override
//    protected void onDestroy() {
//        Window window = getWindow();
//        window.setStatusBarColor(Color.getIntColor("#757575"));
//        window.setNavigationBarColor(Color.getIntColor("#ffffff"));
//        super.onDestroy();
//    }

    @Override
    public void onTouchStart(Slider slider) {

    }

    @Override
    public void onTouchEnd(Slider slider) {

    }
}
